/*
 * Copyright (C) 2012, EADS France
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */
package xowl.gmi.view.diagram;

/**
 *
 * @author Laurent WOUTERS
 */
public class GraphConnectorEndDiamond implements GraphConnectorEnd {
    private java.awt.Color color;
    private int thickness;
    
    public void setBorderColor(java.awt.Color color) { this.color = color; }
    public void setBorderThickness(int value) { this.thickness = value; }
    
    public GraphConnectorEndDiamond(java.awt.Color borderColor, int thickness) {
        this.color = borderColor;
        this.thickness = thickness;
    }
    
    @Override public Couple paint(java.awt.Graphics2D g2d, Couple offset, Couple f, Couple t, DiagramStyle style) {
        Couple v = (new Couple(f, t)).colinear(style.diamondLength);
        Couple p1 = t.minus(v);
        paintArrowHead(g2d, p1.plus(offset), t.plus(offset), style);
        return p1;
    }
    private void paintArrowHead(java.awt.Graphics2D g2d, Couple f, Couple t, DiagramStyle style) {
        Couple offset = (new Couple(f, t)).orthogonal((double)style.diamondWidth / 2);
        Couple p3 = f.plus((new Couple(f, t)).mult(0.5d)).plus(offset);
        Couple p4 = f.plus((new Couple(f, t)).mult(0.5d)).minus(offset);
        java.awt.Polygon polygon = new java.awt.Polygon(new int[] {(int)f.x, (int)p3.x, (int)t.x, (int)p4.x}, new int[] {(int)f.y, (int)p3.y, (int)t.y, (int)p4.y}, 4);
        g2d.setColor(style.shapeBackgroundColor);
        g2d.fillPolygon(polygon);
        g2d.setColor((color!=null)? color: style.connectorColor);
        g2d.setStroke(new java.awt.BasicStroke((thickness!=0)? thickness: style.connectorThickness));
        g2d.drawPolygon(polygon);
    }
    
    @Override public Couple exportSVG(org.w3c.dom.Document doc, org.w3c.dom.Element parent,Couple offset, Couple f, Couple t, DiagramStyle style) {
        Couple v = (new Couple(f, t)).colinear(style.diamondLength);
        Couple p1 = t.minus(v);
        parent.appendChild(exportArrowHead(doc, p1.plus(offset), t.plus(offset), style));
        return p1;
    }
    private org.w3c.dom.Element exportArrowHead(org.w3c.dom.Document doc, Couple f, Couple t, DiagramStyle style) {
        Couple offset = (new Couple(f, t)).orthogonal((double)style.diamondWidth / 2);
        Couple p3 = f.plus((new Couple(f, t)).mult(0.5d)).plus(offset);
        Couple p4 = f.plus((new Couple(f, t)).mult(0.5d)).minus(offset);
        
        java.lang.StringBuilder builder = new java.lang.StringBuilder();
        builder.append(f.x); builder.append(","); builder.append(f.y);
        builder.append(" "); 
        builder.append(p3.x); builder.append(","); builder.append(p3.y);
        builder.append(" "); 
        builder.append(t.x); builder.append(","); builder.append(t.y);
        builder.append(" "); 
        builder.append(p4.x); builder.append(","); builder.append(p4.y);
        
        org.w3c.dom.Element elem = doc.createElement("polygon");
        elem.setAttribute("points", builder.toString());
        java.awt.Color fill = style.shapeBackgroundColor;
        java.awt.Color border = (color!=null)? color: style.connectorColor;
        int stroke = (thickness!=0)? thickness: style.connectorThickness;
        if (fill != null && fill.getAlpha() != 0) {
            elem.setAttribute("fill", "#" + Widget.getColorString(fill));
            if (fill.getAlpha() != 255) {
                float value = ((float)fill.getAlpha()) / 255;
                elem.setAttribute("fill-opacity", Float.toString(value));
            }
        }
        if (stroke > 0 && border != null && border.getAlpha() != 0) {
            elem.setAttribute("stroke", "#" + Widget.getColorString(border));
            if (border.getAlpha() != 255) {
                float value = ((float)border.getAlpha()) / 255;
                elem.setAttribute("stroke-opacity", Float.toString(value));
            }
            elem.setAttribute("stroke-width", Integer.toString(stroke));
        }
        return elem;
    }
}
